import 'package:flutter/cupertino.dart';
import 'package:read_book/data/book_mall/book_mall.dart';

import '../../widget/book_des.dart';
import '../../widget/swiper.dart';

class Ranking extends StatefulWidget {
  final tabsName;

  const Ranking({Key key, this.tabsName}) : super(key: key);

  @override
  _RankingState createState() => _RankingState();
}

class _RankingState extends State<Ranking> {
  //排行id
  final String totalRank = "564d8494fe996c25652644d2";
  List books;
  Map data;

  //获取排行榜数据
  void getRankingData() {
    BookMall().getRankingList(totalRank).then((value) => setState(() {
          books = value['ranking']['books'];
        }));
  }

  //获取男生数据
  void getTypeData(params) {
    //请求数据
    BookMall().getTypeData(params).then((value) => setState(() {
          books = value['books'];
        }));
  }

  @override
  void initState() {
    Map<String, dynamic> params = {
      'gender': 'male',
      'major': '玄幻',
      'type': 'hot',
      'minor': '',
      'start': '0',
      'limit': '20'
    };
    //排行榜
    if (widget.tabsName == 'ranking') {
      getRankingData();
    }
    if (widget.tabsName == 'man') {
      params['gender'] = 'male';
      params['major'] = '玄幻';
      getTypeData(params);
    }
    if (widget.tabsName == 'woman') {
      params['gender'] = 'female';
      params['major'] = '古代言情';
      getTypeData(params);
    }
    if (widget.tabsName == '出版') {
      params['gender'] = 'press';
      params['major'] = '传记名著';
      getTypeData(params);
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: <Widget>[
        SizedBox(
          child: SwiperPage(books: books),
          height: 200.0,
        ),
        Container(
          child: bookList(),
        )
      ],
    );
  }

  Widget bookList() {
    return ListView.builder(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(), //禁用滑动事件
        itemCount: books.length,
        itemBuilder: (BuildContext context, int index) {
          return Container(
            padding: const EdgeInsets.only(left: 10),
            child: BookDes(book: books[index]),
            decoration: const BoxDecoration(
                border: Border(
                    bottom: BorderSide(width: 1, color: Color(0xffe5e5e5)))),
          );
        });
  }
}
